Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-aria/select

Package Overview
Dependencies
Maintainers
2
Versions
773
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-aria/select

Spectrum UI components in React

  • 3.14.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is @react-aria/select?

@react-aria/select is a React library that provides accessible components for building custom select menus. It is part of the React Aria collection, which focuses on providing high-quality, accessible UI components.

What are @react-aria/select's main functionalities?

Basic Select

This code demonstrates a basic select component using @react-aria/select. It includes a label, a button to trigger the select menu, and a list of items to choose from.

import { useSelectState } from '@react-stately/select';
import { useSelect } from '@react-aria/select';
import { Item } from '@react-stately/collections';

function SelectExample() {
  let state = useSelectState({
    items: [
      { key: 'red', name: 'Red' },
      { key: 'green', name: 'Green' },
      { key: 'blue', name: 'Blue' }
    ]
  });

  let ref = React.useRef();
  let { labelProps, triggerProps, valueProps, menuProps } = useSelect({
    label: 'Favorite Color',
    items: state.collection,
    selectedKey: state.selectedKey,
    onSelectionChange: state.setSelectedKey
  }, state, ref);

  return (
    <div>
      <label {...labelProps}>Favorite Color</label>
      <button {...triggerProps} ref={ref}>
        <span {...valueProps}>{state.selectedItem ? state.selectedItem.name : 'Select an option'}</span>
      </button>
      {state.isOpen && (
        <ul {...menuProps}>
          {[...state.collection].map(item => (
            <Item key={item.key}>{item.name}</Item>
          ))}
        </ul>
      )}
    </div>
  );
}

Custom Styling

This example shows how to apply custom styling to the select component using CSS classes. The custom styles are defined in an external CSS file.

import { useSelectState } from '@react-stately/select';
import { useSelect } from '@react-aria/select';
import { Item } from '@react-stately/collections';
import './customStyles.css';

function CustomStyledSelect() {
  let state = useSelectState({
    items: [
      { key: 'apple', name: 'Apple' },
      { key: 'banana', name: 'Banana' },
      { key: 'cherry', name: 'Cherry' }
    ]
  });

  let ref = React.useRef();
  let { labelProps, triggerProps, valueProps, menuProps } = useSelect({
    label: 'Favorite Fruit',
    items: state.collection,
    selectedKey: state.selectedKey,
    onSelectionChange: state.setSelectedKey
  }, state, ref);

  return (
    <div className="custom-select">
      <label {...labelProps}>Favorite Fruit</label>
      <button {...triggerProps} ref={ref} className="custom-trigger">
        <span {...valueProps}>{state.selectedItem ? state.selectedItem.name : 'Select an option'}</span>
      </button>
      {state.isOpen && (
        <ul {...menuProps} className="custom-menu">
          {[...state.collection].map(item => (
            <Item key={item.key} className="custom-item">{item.name}</Item>
          ))}
        </ul>
      )}
    </div>
  );
}

Other packages similar to @react-aria/select

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc